<template>
	<view class="page">
		<u-navbar title="收货地址" @leftClick='jump("my")' :placeholder="true" border>
		</u-navbar>
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="loading">
					<view style="height: 30rpx;margin-top: 10rpx;" v-for="item in 30" :key="item" class="Skeleton" />
				</template>
				<template v-else>
					<view @click="radiovalue=item.id" v-for="item in data" :key='item.id' class="contentBox viewWidth"
						:class="{'chooseAddress':radiovalue==item.id}">
						<view class="flex">
							<view class="mr10">
								<u-text lines='1' :text='item.name' :bold='true' size='30rpx' />
							</view>
							<view>
								<u-text :text='desensitizePhoneNumber(item.phone)' mode='phone' :bold='true'
									size='30rpx' />
							</view>
							<view class="default" v-if="item.is_default" style="margin-left: 20rpx;">默认</view>
							<view class="tag" style="margin-left: 20rpx;" v-if="item.address_tag">{{item.address_tag}}
							</view>
						</view>
						<view style="margin: 20rpx 0;">
							<u-text :text='item.address_district' size='28rpx' />
						</view>
						<view class='flex'>
							<view style="flex: 1;" class='flex'>
								<view class="mr10 checkbox" :class="{'choose':item.is_default}"
									@click="change(item.id)">
									<u-icon name="checkmark" color='#fff'></u-icon>
								</view>
								<text @click="change(item.id)"
									:style="{color:item.is_default?'#F01111':'#000'}">{{item.is_default?'已默认':'设置默认'}}</text>
							</view>
							<view class="but" @click="jump('修改地址',item.id)">
								编辑
							</view>
							<view class="but" @click="del(item.id)">
								删除
							</view>
						</view>
					</view>
				</template>
			</z-paging>
		</view>
		<view class="footer viewWidth">
			<u-button color='#F01111' :disabled='disabled' type="primary" text="新增收获地址" @click="jump('新增地址')"
				shape="circle"></u-button>
		</view>
		<view class="footer viewWidth" style="margin-top:10rpx;">
			<u-button color='#F01111' :disabled='disabled' type="primary" text="确认" @click="jump"
				shape="circle"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				disabled: false,
				loading: true,
				data: [],
				product_id: null,
				radiovalue: null,
				op: null
			}
		},
		onLoad(op) {
			this.product_id = op.id
			this.op = op
		},
		methods: {
			desensitizePhoneNumber(phoneNumber) {
				// 获取手机号长度
				const length = phoneNumber.length;

				// 如果手机号长度小于等于7，则无需脱敏，直接返回原始手机号
				if (length <= 7) {
					return phoneNumber;
				}

				// 取出前三位
				const firstThree = phoneNumber.substring(0, 3);
				// 取出后四位
				const lastFour = phoneNumber.substring(length - 4);
				// 构造脱敏字符串，中间四位用 "*" 替换
				const middle = "*".repeat(length - 7);

				// 组合成脱敏后的手机号
				const desensitizedPhoneNumber = firstThree + middle + lastFour;

				return desensitizedPhoneNumber;
			},
			jump(str, id = '') {
				if (this.disabled) return
				if (str == '新增地址') {
					this.$utils.navigateTo({
						url: '/address/addressPage/addressPage?type=' + str + '&jump=select'
					})
				} else if (str == '修改地址') {
					this.$utils.navigateTo({
						url: '/address/addressPage/addressPage?type=' + str + '&id=' + id + '&jump=select'
					})
				} else {
					if (this.op.type) {
						this.disabled = true
						uni.showLoading({
							title: '更改中...'
						})
						this.$api.Order.Address(this.product_id, {
							address_id: this.radiovalue
						}).then(res => {
							uni.hideLoading()
							uni.$u.toast('更改成功')
							setTimeout(() => {
								uni.redirectTo({
									url: '/order/detail?id=' + this.product_id
								})
							}, 500)
						}).catch(err => {
							this.disabled = false
							uni.hideLoading()
							uni.$u.toast(err.data.message)
						})

					} else {
						uni.redirectTo({
							url: '/product/pay?id=' + this.product_id + '&selectAddressId=' + this.radiovalue
						})
					}

				}
			},
			async query(page, size) {
				const res = await this.$api.My.addresses()
				if (res) {
					this.$refs.paging.completeByNoMore(res.data.data, true)
					this.loading = false
					this.radiovalue = ~~this.op.address_id
				}
			},
			change(e) {
				console.log(e)
				let item = this.data.filter(item => item.id == e)[0]
				if (item.is_default) return
				this.updata({
					...item,
					is_default: 1
				})
			},
			updata(params) {
				this.$api.address.put(params.id, params)
				this.data = this.data.map(item => {
					if (item.id == params.id) {
						return {
							...item,
							is_default: 1
						}
					}
					return {
						...item,
						is_default: 0
					}
				})
			},
			async del(id) {
				const res = await this.$api.address.delete(id)
				if (res) {
					this.query()
					uni.$u.toast(res.message)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.but {
		background-color: #ddd;
		border-radius: 100rpx;
		margin-right: 10rpx;
		padding: 10rpx 20rpx;
	}

	.tag {
		padding: 0 5rpx;
		border-radius: 6rpx;
		border: 1rpx solid #4E90EA;
		color: #4E90EA;
		font-size: 20rpx;
	}

	.default {
		padding: 0 5rpx;
		border-radius: 6rpx;
		border: 1rpx solid #F01111;
		color: #F01111;
		font-size: 20rpx;
		width: 40rpx;
		text-align: center;
	}

	.footer {
		height: 80rpx;
	}

	.page {
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		background-color: #f5f5f5;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.contentBox {
		background-color: #fff;
		margin-top: 20rpx;
		border-radius: 16rpx;
		padding: 20rpx;
		box-sizing: border-box;

		.flex {
			display: flex;
			align-items: center;
		}
	}

	.mr10 {
		margin-right: 10rpx;
	}

	.checkbox {
		border-radius: 8rpx;
		border: 1rpx solid #bbb;
		background-color: #fff;
		width: 30rpx;
		height: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.choose {
		background-color: #F01111;
		border: 1rpx solid #F01111;
	}

	.chooseAddress {
		border: 1rpx solid #F01111;
	}
</style>